/**
 *  Copyright 2009 Henning Teek, Ingo Renner
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */
package com.googlecode.chigs.domain;

import org.synyx.hades.domain.AbstractPersistable;

import com.googlecode.chigscommons.domain.DataFileContainer;

import javax.persistence.*;
import java.util.List;

/**
 * @author Ingo Renner
 * @author Henning Teek
 */
@Entity
@NamedQueries({
	@NamedQuery(name = "Attachment.findByAll", query = "from Attachment o"),
    @NamedQuery(name = "Attachment.findByRevision", query = "select o from Attachment o, in (o.revisions) r where r = ?")})	
public class Attachment extends AbstractPersistable<Long> implements DataFileContainer {
	private String filename;
	private long size;
	private String hash;
	private String description;
	
	@ManyToMany(mappedBy = "attachments")
	private List<Revision> revisions;

	@ManyToOne
	private ConfigLocation configLocation;
	
	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public void setSize(long size) {
		this.size = size;
	}

	public long getSize() {
		return size;
	}

	public String getHash() {
		return hash;
	}

	public void setHash(String hash) {
		this.hash = hash;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public ConfigLocation getConfigLocation() {
		return configLocation;
	}

	public void setConfigLocation(ConfigLocation configLocation) {
		this.configLocation = configLocation;
	}

	@Override
	public String toString() {
		return "Attachment [configLocation=" + configLocation
				+ ", description=" + description + ", filename=" + filename
				+ ", hash=" + hash + ", size=" + size + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((configLocation == null) ? 0 : configLocation.hashCode());
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result
				+ ((filename == null) ? 0 : filename.hashCode());
		result = prime * result + ((hash == null) ? 0 : hash.hashCode());
		result = prime * result
				+ ((revisions == null) ? 0 : revisions.hashCode());
		result = prime * result + (int) (size ^ (size >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (!(obj instanceof Attachment))
			return false;
		Attachment other = (Attachment) obj;
		if (configLocation == null) {
			if (other.configLocation != null)
				return false;
		} else if (!configLocation.equals(other.configLocation))
			return false;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (filename == null) {
			if (other.filename != null)
				return false;
		} else if (!filename.equals(other.filename))
			return false;
		if (hash == null) {
			if (other.hash != null)
				return false;
		} else if (!hash.equals(other.hash))
			return false;
		if (revisions == null) {
			if (other.revisions != null)
				return false;
		} else if (!revisions.equals(other.revisions))
			return false;
        return size == other.size;
		}

}
